<template>
	<view>
		<!-- 自动换行待实现 -->
		<view class="uni-textarea">
			<textarea v-model="share.textMessage"  placeholder="这一刻的想法..."  />
			</view>
		<uni-file-picker ref="fileImage" :limit="9" :auto-upload="false" fileMediatype="image"
			mode="grid" @select="select" @progress="progress" @success="success" @delete="deleteImage" @fail="fail"></uni-file-picker>
		<view class="submit-btn">
			<button type="primary" @click="publish"> 发布</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mediaList: [],
                share:{},
			};
		},
		onLoad() {
            this.share.userId = uni.getStorageSync("user").id;
            // console.log(this.share.userId);
		},
        methods:{
            select(e){
                console.log("select");
            	const tempFilePaths = e.tempFilePaths;//e是获取的图片源
            	uni.uploadFile({
            		url: '/gongyi/common/upload', //上传图片的后端接口
            		filePath: tempFilePaths[0],
            		name: 'file',
            		success: res => {
            			this.share.pictureUrl = JSON.parse(res.data).name;
            			// console.log(this.share.pictureUrl);
            		}
                })
            },
            deleteImage(){
              uni.request({
                  url:'/gongyi/common/delete?name='+this.share.pictureUrl,
                  method:'DELETE',
                  success: () => {
                       uni.showToast({
                           title:'图片移除成功！',
                           icon:'success'
                       })
                  }
              })  
            },
            async publish(){
                console.log(this.share)
                await uni.request({
                    url:'/gongyi/share/addShare',
                    method:'POST',
                    data:JSON.stringify(this.share),
                    success: (res) => {
                        uni.switchTab({
                            url:'/pages/taolun/taolun'
                        })
                    }
                }) 
            },
            progress(){
                console.log("progress");
            },
            success(){
                console.log("success");
            }
        }
	}
</script>

<style lang="scss">

	.uni-textarea {
		width: auto;
		padding: 50rpx 25rpx;
		line-height: 1.6;
		height: 150rpx;
		padding-bottom: 80rpx;
	}
	.submit-btn {
			display: flex;
			align-items: center;
			height: 35rpx;
			line-height: 35rpx;
			margin-left: 10rpx;
	}

</style>
